var cusUnique;//用户唯一性标识
var shopUnique;//店铺唯一性标识
var cusFlag;//

var imgUrl="http://121.42.189.191:8080/";//全局变量

$(document).ready(function() {
	var thisURL = document.URL;    
	var  getval =thisURL.split('?')[1].split('&'); 
	cusUnique=getval[0].split("=")[1];//获取用户唯一性标识
	shopUnique=getval[1].split("=")[1];//获取店铺唯一性标识
	cusFlag=getval[2].split("=")[1];//获取用户身份
	console.log("cusUnique;"+cusUnique+";shopUnique"+shopUnique+";cusFlag:"+cusFlag);
	goodsKind(shopUnique);//初始化加载商品分类信息
	sumBasket();//加载购物车数量
	searchGoods(shopUnique, "none", "none");

});


/**
 * 动态查询用户购物车中的商品数量
 */
function sumBasket() {
	$.ajax({
		url : "../basket/findSumBasket.do",
		type : "post",
		data : {
			"cusId" : cusUnique,"shopUnique":shopUnique
		},
		dataType : "json",
		success : function(result) {
			if (result.status == 1) {// 查询成功
				$("#shopcart-number").text(result.data);
			} else {
				$("#shopcart-number").text(0);
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

		}
	});

}


//根据商铺的唯一性标识查询出对应的商品分类信息
function goodsKind(shopUnique){
	$.ajax({
		url : "../goods/findGoodsKind.do",
		type : "post",
		data : {
			"shopUnique" : shopUnique
		},
		dataType : "json",
		success : function(result) {
			console.log(result);
			if (result.status == 1) {// 成功
				var data = result.data;
				$("#slide-goodkind").empty();
				var ul = '<li class="mui-table-view-cell"><a onclick="findGoodsByKind(this)"><span>全部</span><span style="display:none" class="goodsKindUnique">101</span></a></li>';
				for (var a = 0; a < data.length; a++) {
					var li = "";
					li += ' <li class="mui-table-view-cell"><a onclick="findGoodsByKind(this)"><span>'+data[a].goodsKindGroup+'</span><span style="display:none" class="goodsKindUnique">'+data[a].goodsKindUnique+'</span></a></li>'
					ul += li;
				}
				$("#slide-goodkind").append(ul);
				
			} else  {// 用户名错
				
			} 
		}
	});
	
}


//添加进购物车
function addCart(add) {
	if (!cusUnique) {
		return;
	}
	var shoppingCartCount = 1;
	var goodsBarcode=$(add).prev().text();
	console.log("goodsBarcode:"+goodsBarcode+";cusUnique:"+cusUnique+";shoppingCartCount:"+shoppingCartCount+";shopUnique:"+shopUnique+";cusFlag:"+cusFlag);
	
	$.ajax({
		url : "../basket/addBasket.do",
		type : "post",
		data : {
			"goodsBarcode" : goodsBarcode,
			"cusUnique" : cusUnique,
			"shoppingCartCount" : shoppingCartCount,
			"shopUnique" : shopUnique,
			"cusFlag":0
		},
		dataType : "json",
		success : function(result) {
			if (result.status == 1) {// 查询成功
				$('#alert-finish').text('添加成功');
				$('#alert-finish').slideToggle(100);
				setTimeout(function() {
					$('#alert-finish').slideUp(200);
				}, 2000);
				sumBasket();

			} else {

			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {

		}
	});

}


//跳转购物车页面
function goShopcar(){
	window.location.href="shopcar.html?cusUnique="+cusUnique+"&shopUnique="+shopUnique+"&end="+new Date().getTime();
}



//根据商品种类查询商品信息
function findGoodsByKind(kin){
	var goodsKind=$(kin).find("span.goodsKindUnique").text();
	if(goodsKind=="101"){
		goodsKind="none";
	}
	console.log($(kin).parent().siblings().length);
	$(kin).parent().siblings().find("a").removeClass("mui-table-view-divider");
	$(kin).addClass("mui-table-view-divider");

	searchGoods(shopUnique, "none", goodsKind);
}

//刷新加载商品信息
function loadGoods(){
	searchGoods(shopUnique, "none", "none");
}

//搜索框查询商品信息
function serchGoods(){
	var searchBar=$("#searchBoxL").val().trim().toUpperCase();
	searchGoods(shopUnique, searchBar, "none");
}






//选择店铺后加载商品信息

// 手动查询商品信息
function searchGoods(shopUnique, searchBar, goodsKind) {
	console.log("shopUnique:"+shopUnique+";searchBar:"+searchBar+";goodsKind:"+goodsKind)
	var totals = 0;
	if (shopUnique == null) {
		window.location.href = "index.html";
	}
	
	$
			.ajax({
				url : "../online/findGoodsBySearch.do",
				type : "post",
				data : {
					"goodsName" : searchBar,
					"shopUnique" : shopUnique,
					"goodsKind" : goodsKind,
					"pages" : 0,
					"perpage" : 60
				},
				dataType : "json",
				success : function(result) {
					console.log(result);
					if (result.status == 1) {// 查询成功
						$("#pageGoodLists").show();
						totals = result.totals;
						$("#kindGoods").empty();
						console.log("totals:" + totals
								+ "Math.floor(totals/20):"
								+ Math.floor(totals / 20));
						var data = result.data;
						var goods = "";
						for (var a = 0; a < data.length; a++) {
							
							var good = "";
							good += '<div class="lie clearfloat">'
									+ '<a href="detail.html?goodsId='+data[a].goods_id+'&cusFlag='+1+'">'
									+ '<div class="tu clearfloat fl">'
									+ '<img src="'+imgUrl+data[a].goods_picturepath+'"/>'
									+ '</div>'
									+ '</a>'
									+ '<div class="right clearfloat fl">'
									+ '<a href="detail.html?goodsId='+data[a].goods_id+'&cusFlag='+cusFlag+'">'
									+ '<p class="tit">'+data[a].goods_name+'</p>'
									+ '</a>'
									+ '<div class="xia clearfloat">'
									+ '<a>'
									+ '<p class="jifen fl over">¥:<span>'+data[a].goods_sale_price+'</span></p>'
									+ '</a>'
									+ '<span class="listGoodsBarcod" style="display:none">'+data[a].goods_barcode+'</span>'
									+ '<span onclick="addCart(this)" class="fr db"><img src="img/jia.png"/></span>'
									+ '</div>' + '</div>' + '</div>';
							goods += good;
						}
						$("#kindGoods").append(goods);
						$('#pageGoodLists').jqPagination({
							link_string: '/?page={page_number}',
							current_page: 1, //设置当前页 默认为1
							max_page: Math.floor((totals-1)/20)+1, //设置最大页 默认为1
							page_string: '第{current_page}页,共{max_page}页',
							paged: function(page) {
								$
								.ajax({
									url : "../online/findGoodsBySearch.do",
									type : "post",
									data : {
										"goodsName" : searchBar,
										"shopUnique" : shopUnique,
										"goodsKind" : goodsKind,
										"pages" : page,
										"perpage" : 20
									},
									dataType : "json",
									success : function(result) {
										if (result.status == 1) {// 查询成功
											$("#kindGoods").empty();
											var data = result.data;
											var goods = "";
											for (var a = 0; a < data.length; a++) {
												
												var good = "";
												good += '<div class="lie clearfloat">'
														+ '<a href="detail.html?goodsId='+data[a].goods_id+'&cusFlag='+1+'">'
														+ '<div class="tu clearfloat fl">'
														+ '<img src="'+imgUrl+data[a].goods_picturepath+'"/>'
														+ '</div>'
														+ '</a>'
														+ '<div class="right clearfloat fl">'
														+ '<a href="detail.html?goodsId='+data[a].goods_id+'&cusFlag='+1+'">'
														+ '<p class="tit">'+data[a].goods_name+'</p>'
														+ '</a>'
														+ '<div class="xia clearfloat">'
														+ '<a>'
														+ '<p class="jifen fl over">¥:<span>'+data[a].goods_sale_price+'</span></p>'
														+ '</a>'
														+ '<span class="listGoodsBarcod" style="display:none">'+data[a].goods_barcode+'</span>'
														+ '<span onclick="addCart(this)" class="fr db"><img src="img/jia.png"/></span>'
														+ '</div>' + '</div>' + '</div>';
												goods += good;
											}
											$("#kindGoods").append(goods);
										} else {
											
											$("#shop-goods").empty();
											var good="";
											good = '<div class="list clearfloat fl">' + '<a href="#">'
											+ '<div class="tu clearfloat">'
											+ '<img src="upload/hot.jpg" />' + '</div>'
											+ '<div class="bottom clearfloat box-s">'
											+ '<p class="over">看看其他商品吧</p>' + '<span></span>'
											+ '</div>' + '</a>' + '</div>';
											
										}
									},
									error : function(XMLHttpRequest, textStatus, errorThrown) {

									}
								});
								
							}
						});//jqPagination-------end
						
						
					} else {
						$("#kindGoods").empty();
						var good = "";
						good += '<div class="lie clearfloat">'
								+ '<a>'
								+ '<div class="tu clearfloat fl">'
								+ '<img src="upload/20.jpg"/>'
								+ '</div>'
								+ '</a>'
								+ '<div class="right clearfloat fl">'
								+ '<a>'
								+ '<p class="tit">暂时没有商品</p>'
								+ '</a>'
								+ '<div class="xia clearfloat">'
								+ '<a>'
								+ '<p class="jifen fl over"></span></p>'
								+ '</a>'
								+ '</div>' + '</div>' + '</div>';
						$("#kindGoods").append(good);
						
						$("#pageGoodLists").css("display","none");
						$("#shop-goods").append(good);
						$('#pageGoodLists').jqPagination({
							link_string: '/?page={page_number}',
							current_page: 1, //设置当前页 默认为1
							max_page: 1, //设置最大页 默认为1
							page_string: '第0页',
							paged: function(page) {
								
							}
						});
						
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {

				}
			});

}









